﻿// <copyright file="ChangesetFileFinder.cs" company="Chaminda Chandrasekara">
//     Chaminda Chandrasekara. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace API_LIb
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Text;
    using System.Threading.Tasks;
    using Microsoft.TeamFoundation.Client;
    using Microsoft.TeamFoundation.VersionControl.Client;
        
    /// <summary>
    /// Change-set File Information Retrieve class
    /// </summary>
    public class ChangesetFileFinder
    {
        /// <summary>
        /// The documentation text within the constructor's summary tag must begin with the text: Initializes a new instance of the <see cref="ChangesetFileFinder" /> class.
        /// </summary>
        /// <param name="tfsCollectionUrl">TFS Collection URL</param>
        /// <param name="tfsUserDomain">TFS User Domain</param>
        /// <param name="tfsUser">TFS User Name</param>
        /// <param name="tfsUserPassword">TFS User Password</param>
        public ChangesetFileFinder(string tfsCollectionUrl, string tfsUserDomain, string tfsUser, string tfsUserPassword)
        {
            this.TfsCollectionUrl = tfsCollectionUrl;
            this.TfsUserDomain = tfsUserDomain;
            this.TfsUser = tfsUser;
            this.TfsUserPassword = tfsUserPassword;
        }      

        /// <summary>
        /// Gets or sets Team Foundation Server Collection URL
        /// </summary>
        private string TfsCollectionUrl { get; set; }

        /// <summary>
        /// Gets or sets Team Foundation Server User Name
        /// </summary>
        private string TfsUser { get; set; }

        /// <summary>
        /// Gets or sets Team Foundation Server User Domain
        /// </summary>
        private string TfsUserDomain { get; set; }

        /// <summary>
        /// Gets or sets Team Foundation Server User Password
        /// </summary>
        private string TfsUserPassword { get; set; }

        /// <summary>
        /// Get Change-set File Information
        /// </summary>       
        /// <param name="versionControlPath">Version Control Path</param>
        /// <param name="fromChangeset">From Change-set Number</param>
        /// <param name="toChangeset">To Change-set Number</param>
        /// <returns>Change-set File Information</returns>
        public List<ChangesetFileInfo> GetChangesetFiles(string versionControlPath, int fromChangeset, int toChangeset)
        {
            NetworkCredential netCred = new NetworkCredential(
               this.TfsUser,
               this.TfsUserPassword);

            List<ChangesetFileInfo> changesetFiles = new List<ChangesetFileInfo>();
            using (TfsTeamProjectCollection tfsTPC = new TfsTeamProjectCollection(new Uri(this.TfsCollectionUrl), netCred))
            {
                tfsTPC.EnsureAuthenticated();

                VersionSpec fromVersion = new ChangesetVersionSpec(fromChangeset);
                VersionSpec toVersion = new ChangesetVersionSpec(toChangeset);

                VersionControlServer versionControl = (VersionControlServer)tfsTPC.GetService(typeof(VersionControlServer));

                // using versionControl API you can query histroy for changes set 
                IEnumerable changesets = versionControl.QueryHistory(versionControlPath, VersionSpec.Latest, 0, RecursionType.Full, null, fromVersion, toVersion, int.MaxValue, true, true);
                
                foreach (Changeset changeset in changesets)
                {
                    foreach (Change change in changeset.Changes)
                    {
                        changesetFiles.Add(new ChangesetFileInfo() { ChangesetNumber = changeset.ChangesetId, FileName = change.Item.ServerItem });
                    }
                }
            }

            return changesetFiles;
        }
    }
}
